/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.OrderInfoPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  OrderInfoPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class OrderInfoSqlProvider {
	public static final String TABLE_NAME = "order_info";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param orderInfo
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderInfos(final OrderInfoPo orderInfo) {
		return new CustomerSQL() {{
			SELECT("amount_pay,amount_total,bank,bank_account,batch,code,contacts,create_time,creator_id,id,invoice_content,invoice_title,invoice_type,order_source,order_time,pay_method_id,pay_method_name,pay_time,phone,remark,shipping_address,status,status_name,taxpayer_code,update_time,updater_id,user_id,user_name,shipping_time,score, score_promote,shipping_method ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(orderInfo.getCode())) {
				WHERE("code LIKE CONCAT('%',#{code},'%' ) ");
			}
			
			if (null != orderInfo.getOrderSource()) {
				WHERE("order_source=#{orderSource}");
			}
			if (null != orderInfo.getOrderTimeStart()) {
				WHERE("order_time >= #{orderTimeStart}");
			}
			if (null != orderInfo.getOrderTimeEnd()) {
				WHERE("order_time <= #{orderTimeEnd}");
			}
			if (null != orderInfo.getPayMethodId()) {
				WHERE("pay_method_id=#{payMethodId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getPhone())) {
				WHERE("phone=#{phone}");
			}
			if (null != orderInfo.getStatus() && orderInfo.getStatus() > 0) {
				WHERE("status=#{status}");
			}
			if (null != orderInfo.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getUserName())) {
				WHERE("user_name LIKE CONCAT('%',#{userName},'%' ) ");
			}
			if(StringUtils.isNotBlank(orderInfo.getSortField())) {
				ORDER_BY(orderInfo.getSortField() + (StringUtils.isNotBlank(orderInfo.getSortMethod()) ? "desc" : orderInfo.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != orderInfo.getStart() && null != orderInfo.getSize()) {
				LIMIT(orderInfo.getStart(), orderInfo.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param orderInfo
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderInfoCount(final OrderInfoPo orderInfo) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(orderInfo.getCode())) {
				WHERE("code LIKE CONCAT('%',#{code},'%' ) ");
			}
			
			if (null != orderInfo.getOrderSource()) {
				WHERE("order_source=#{orderSource}");
			}
			if (null != orderInfo.getOrderTimeStart()) {
				WHERE("order_time >= #{orderTimeStart}");
			}
			if (null != orderInfo.getOrderTimeEnd()) {
				WHERE("order_time <= #{orderTimeEnd}");
			}
			if (null != orderInfo.getPayMethodId()) {
				WHERE("pay_method_id=#{payMethodId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getPhone())) {
				WHERE("phone=#{phone}");
			}
			if (null != orderInfo.getStatus()) {
				WHERE("status=#{status}");
			}
			if (null != orderInfo.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getUserName())) {
				WHERE("user_name LIKE CONCAT('%',#{userName},'%' ) ");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findOrderInfoById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param orderInfo
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addOrderInfo(final OrderInfoPo orderInfo) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != orderInfo.getAmountPay()) {
				VALUES("amount_pay","#{amountPay}");
			}
			if (null != orderInfo.getAmountTotal()) {
				VALUES("amount_total","#{amountTotal}");
			}
			if (StringUtils.isNotBlank(orderInfo.getBank())) {
				VALUES("bank","#{bank}");
			}
			if (StringUtils.isNotBlank(orderInfo.getBankAccount())) {
				VALUES("bank_account","#{bankAccount}");
			}
			if (StringUtils.isNotBlank(orderInfo.getBatch())) {
				VALUES("batch","#{batch}");
			}
			if (StringUtils.isNotBlank(orderInfo.getCode())) {
				VALUES("code","#{code}");
			}
			if (StringUtils.isNotBlank(orderInfo.getContacts())) {
				VALUES("contacts","#{contacts}");
			}
			if (null != orderInfo.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getInvoiceContent())) {
				VALUES("invoice_content","#{invoiceContent}");
			}
			if (StringUtils.isNotBlank(orderInfo.getInvoiceTitle())) {
				VALUES("invoice_title","#{invoiceTitle}");
			}
			if (null != orderInfo.getInvoiceType()) {
				VALUES("invoice_type","#{invoiceType}");
			}
			if (null != orderInfo.getOrderSource()) {
				VALUES("order_source","#{orderSource}");
			}
			if (null != orderInfo.getOrderTime()) {
				VALUES("order_time","#{orderTime}");
			}
			if (null != orderInfo.getPayMethodId()) {
				VALUES("pay_method_id","#{payMethodId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getPayMethodName())) {
				VALUES("pay_method_name","#{payMethodName}");
			}
			if (null != orderInfo.getPayTime()) {
				VALUES("pay_time","#{payTime}");
			}
			if (StringUtils.isNotBlank(orderInfo.getPhone())) {
				VALUES("phone","#{phone}");
			}
			if (StringUtils.isNotBlank(orderInfo.getRemark())) {
				VALUES("remark","#{remark}");
			}
			if (StringUtils.isNotBlank(orderInfo.getShippingAddress())) {
				VALUES("shipping_address","#{shippingAddress}");
			}
			if (null != orderInfo.getStatus()) {
				VALUES("status","#{status}");
			}
			if (StringUtils.isNotBlank(orderInfo.getStatusName())) {
				VALUES("status_name","#{statusName}");
			}
			if (StringUtils.isNotBlank(orderInfo.getTaxpayerCode())) {
				VALUES("taxpayer_code","#{taxpayerCode}");
			}
			if (null != orderInfo.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (null != orderInfo.getUserId()) {
				VALUES("user_id","#{userId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getUserName())) {
				VALUES("user_name","#{userName}");
			}
			if (StringUtils.isNotBlank(orderInfo.getShippingTime())) {
				VALUES("shipping_time","#{shippingTime}");
			}
			if (StringUtils.isNotBlank(orderInfo.getShippingMethod())) {
				VALUES("shipping_method","#{shippingMethod}");
			}
			if (null != orderInfo.getScore()) {
				VALUES("score","#{score}");
			}
			if (null != orderInfo.getScorePromote()) {
				VALUES("score_promote","#{scorePromote}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param orderInfo
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateOrderInfo(final OrderInfoPo orderInfo) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (null != orderInfo.getAmountPay()) {
				SET("amount_pay=#{amountPay}");
			}
			if (null != orderInfo.getAmountTotal()) {
				SET("amount_total=#{amountTotal}");
			}
			if (StringUtils.isNotBlank(orderInfo.getBank())) {
				SET("bank=#{bank}");
			}
			if (StringUtils.isNotBlank(orderInfo.getBankAccount())) {
				SET("bank_account=#{bankAccount}");
			}
			if (StringUtils.isNotBlank(orderInfo.getBatch())) {
				SET("batch=#{batch}");
			}
			if (StringUtils.isNotBlank(orderInfo.getCode())) {
				SET("code=#{code}");
			}
			if (StringUtils.isNotBlank(orderInfo.getContacts())) {
				SET("contacts=#{contacts}");
			}
			if (null != orderInfo.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getInvoiceContent())) {
				SET("invoice_content=#{invoiceContent}");
			}
			if (StringUtils.isNotBlank(orderInfo.getInvoiceTitle())) {
				SET("invoice_title=#{invoiceTitle}");
			}
			if (null != orderInfo.getInvoiceType()) {
				SET("invoice_type=#{invoiceType}");
			}
			if (null != orderInfo.getOrderSource()) {
				SET("order_source=#{orderSource}");
			}
			if (null != orderInfo.getOrderTime()) {
				SET("order_time=#{orderTime}");
			}
			if (null != orderInfo.getPayMethodId()) {
				SET("pay_method_id=#{payMethodId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getPayMethodName())) {
				SET("pay_method_name=#{payMethodName}");
			}
			if (null != orderInfo.getPayTime()) {
				SET("pay_time=#{payTime}");
			}
			if (StringUtils.isNotBlank(orderInfo.getPhone())) {
				SET("phone=#{phone}");
			}
			if (StringUtils.isNotBlank(orderInfo.getRemark())) {
				SET("remark=#{remark}");
			}
			if (StringUtils.isNotBlank(orderInfo.getShippingAddress())) {
				SET("shipping_address=#{shippingAddress}");
			}
			if (null != orderInfo.getStatus()) {
				SET("status=#{status}");
			}
			if (StringUtils.isNotBlank(orderInfo.getTaxpayerCode())) {
				SET("taxpayer_code=#{taxpayerCode}");
			}
			if (null != orderInfo.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (null != orderInfo.getUserId()) {
				SET("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(orderInfo.getUserName())) {
				SET("user_name=#{userName}");
			}
			if (StringUtils.isNotBlank(orderInfo.getShippingTime())) {
				SET("shipping_time=#{shippingTime}");
			}
			if (StringUtils.isNotBlank(orderInfo.getShippingMethod())) {
				SET("shipping_method=#{shippingMethod}");
			}
			if (null != orderInfo.getScore()) {
				SET("score=#{score}");
			}
			if (null != orderInfo.getScorePromote()) {
				SET("score_promote=#{scorePromote}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delOrderInfo(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
